package com.szh.taobaounion.ui.activity;

import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.szh.taobaounion.R;
import com.szh.taobaounion.base.BaseActivity;
import com.szh.taobaounion.base.BaseFragment;
import com.szh.taobaounion.ui.fragment.HomeFragment;
import com.szh.taobaounion.ui.fragment.OnSellFragment;
import com.szh.taobaounion.ui.fragment.SearchFragment;
import com.szh.taobaounion.ui.fragment.SelectedFragment;
import com.szh.taobaounion.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements IMainActivity{
    private static final String TAG = "MainActivity";
    @BindView(R.id.main_navigation_bar)
    BottomNavigationView mNavigationView;
    HomeFragment homeFragment;
    SearchFragment searchFragment;
    SelectedFragment selectedFragment;
    OnSellFragment redPacketFragment;
    FragmentManager mFm;

    @Override
    protected void initPersent() {

    }
    public void switch2Search(){
//        switchFragment(searchFragment);
        mNavigationView.setSelectedItemId(R.id.search);
    }

    @Override
    protected void initEvent() {
        initListener();
    }

    @Override
    protected void initView() {
        List<Integer> childIds = new ArrayList<>();
        childIds.add(R.id.home);
        childIds.add(R.id.selected);
        childIds.add(R.id.red_packet_normal);
        childIds.add(R.id.search);
        ViewGroup childAt = (ViewGroup) mNavigationView.getChildAt(0);
        for(int position = 0;position<childIds.size();position++){
            BottomNavigationItemView viewById = childAt.getChildAt(position).findViewById(childIds.get(position));
            viewById.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
        }
        initFragment();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }


    private void initFragment() {
        homeFragment = new HomeFragment();
        selectedFragment = new SelectedFragment();
        redPacketFragment = new OnSellFragment();
        searchFragment = new SearchFragment();
        mFm = getSupportFragmentManager();

        switchFragment(homeFragment);
    }

    private void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(item -> {
            //Log.d(TAG, "title is: "+item.getTitle()+"id is "+item.getItemId());
            if (item.getItemId() == R.id.home) {
                //TODO
                LogUtils.d(this, "切换到了首页 ");
                switchFragment(homeFragment);
            } else if (item.getItemId() == R.id.selected) {
                LogUtils.d(this, "切换到了精选 ");
                switchFragment(selectedFragment);
            } else if (item.getItemId() == R.id.red_packet_normal) {
                LogUtils.d(this, "切换到了特惠");
                switchFragment(redPacketFragment);
            } else if (item.getItemId() == R.id.search) {
                LogUtils.d(this, "切换到了搜索");
                switchFragment(searchFragment);
            }
            return true;
        });
    }
    //上一次现实的Fragment
    private BaseFragment lastFragment=null;

    private void switchFragment(BaseFragment targetFragment) {
        //改成add和hide的方式修改Fragment
        if (lastFragment ==targetFragment) {
            return;
        }
        FragmentTransaction fragmentTransaction = mFm.beginTransaction();
        if (!targetFragment.isAdded()) {
            fragmentTransaction.add(R.id.main_page_container,targetFragment);
        }else {
            fragmentTransaction.show(targetFragment);
        }
        if (lastFragment != null) {
            fragmentTransaction.hide(lastFragment);
        }
        lastFragment=targetFragment;
        fragmentTransaction.commit();
    }



}
